package com.campus.backend.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.campus.backend.service.SessionService;
import com.campus.backend.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Autowired
    private SessionService sessionService;


    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        UserInfoVo userInfo = sessionService.getUserInfo();
        this.strictInsertFill(metaObject, "creatorId", Integer.class, userInfo.getId());
        this.strictInsertFill(metaObject, "creatorName", String.class, userInfo.getRealName());
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());

        this.strictInsertFill(metaObject, "updatorId", Integer.class, userInfo.getId());
        this.strictInsertFill(metaObject, "updatorName", String.class, userInfo.getRealName());
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        UserInfoVo userInfo = sessionService.getUserInfo();
        this.strictInsertFill(metaObject, "updatorId", Integer.class, userInfo.getId());
        this.strictInsertFill(metaObject, "updatorName", String.class, userInfo.getRealName());
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)
    }
}